﻿using Lamex.Base;
using Lamex.SystemManager.BLL;
using Lamex.Library;
using Lamex.SystemManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Lamex.SystemManager.UI
{
    public partial class frmBatchButton : frmShow
    {
        string functionid = "";
        public frmBatchButton(string functionid)
        {
            this.functionid = functionid;
            InitializeComponent();
            string item = "";
            if (string.IsNullOrEmpty(functionid))
                item = this.ucFunction.Value;
            else
                item = functionid;
            ucFunction.Value = functionid;
        }

        private void ucFunction_EditValueChanged(object sender, EventArgs e)
        {
            string item = "";
            if (string.IsNullOrEmpty(functionid))
                item = this.ucFunction.Value;
            else
                item = functionid;
            ucFunction.Value = functionid;
            //if (!string.IsNullOrEmpty(item) && item == "-1")
            //{
            //    SetVisible(true);
            //}
            //else
            //{
            //    SetVisible(false);
            //}
        }
        private void SetVisible(bool visible)
        {
            //this.txtSystemType.Visible = visible;
            //this.lblSystemType.Visible = visible;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ucFunction.Text.Length == 0)
                return;

            #region 验证用户输入

            if (string.IsNullOrEmpty(ucFunction.Text))
            {
                MessageBox.Show("功能ID不能为空");
                this.ucFunction.Focus();
                return;
            }

            #endregion

            string id = this.ucFunction.Value;
            Sys_FunctionInfo functionInfo = BaseFactory<Sys_Function>.Instance.GetEntriyList(" 1=1 and ID='" + id + "'").FirstOrDefault() ;

            if (functionInfo == null) return;
            //{
            //    string filter = string.Format("FunControlID='{0}' and SystemType='{1}'", this.txtFunctionID.Text, functionInfo.SystemType);
            //    List<Sys_FunctionInfo> lst = BaseFactory<Sys_Function>.Instance.GetEntriyList(filter);
            //    if (lst.Count!=0)
            //    {
            //        MessageBox.Show("指定功能控制ID重复，请重新输入！");
            //        this.txtName.Focus();
            //        return;
            //    }
            //}
            //else
            //{
            //    //当新建系统类型的时候
            //    functionInfo = new Sys_FunctionInfo();
            //    functionInfo.PID = "-1";
            //    functionInfo.SystemType = this.txtSystemType.Text;
            //    functionInfo.FunControlID = this.txtFunctionID.Text;

            //    string filter = string.Format("FunControlID='{0}' and SystemType='{1}'", this.txtFunctionID.Text, functionInfo.SystemType);
            //    List<Sys_FunctionInfo> lst = BaseFactory<Sys_Function>.Instance.GetEntriyList(filter);
            //    if (lst.Count!=0)
            //    {
            //        MessageBox.Show("指定功能控制ID重复，请重新输入！");
            //        this.txtName.Focus();
            //        return;
            //    }
            //}

            //Sys_FunctionInfo mainInfo = new Sys_FunctionInfo();
            Sys_FunctionInfo mainInfo = functionInfo;
            //mainInfo = SetFunction(mainInfo);//去除掉
            //mainInfo.FunName = functionInfo.FunName;
            //mainInfo.PID = this.ucFunction.Value;
            //mainInfo.FunControlID = this.txtFunctionID.Text;
            //mainInfo.SystemType = functionInfo.SystemType;//和父节点的SystemType_ID一样。
            //using (DbTransaction trans = BaseFactory<Sys_Function>.Instance)
            //{
            //    try
            //    {
            //        if (trans != null)
            //        {
                        //bool sucess = BaseFactory<Sys_Function>.Instance.Insert(mainInfo);
                        //if (BaseFactory<Sys_Function>.Instance)
                        //{
                            Sys_FunctionInfo subInfo = null;
                            int sortCodeIndex = 1;
                            mainInfo = BaseFactory<Sys_Function>.Instance.GetEntriyList(" FunControlID='" + functionInfo.FunControlID + "' and SystemType='" + functionInfo.SystemType + "' ").FirstOrDefault();
                            #region 子功能操作
            if (chkAdd.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnAdd");
                subInfo.FunName = string.Format("新增");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName =(string) chkAdd.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkDelete.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnDelete");
                subInfo.FunName = string.Format("删除");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkDelete.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkModify.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnEdit");
                subInfo.FunName = string.Format("修改");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkModify.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkSave.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnSave");
                subInfo.FunName = string.Format("保存");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkSave.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkView.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnView");
                subInfo.FunName = string.Format("刷新");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkView.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkImport.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnImport", mainInfo.FunControlID);
                subInfo.FunName = string.Format("导入", mainInfo.FunName);
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkImport.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkExport.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnExport");
                subInfo.FunName = string.Format("导出");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkExport.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkPrint.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnPrint");
                subInfo.FunName = string.Format("打印");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkPrint.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            
            if (chkCalculate.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnCalculate");
                subInfo.FunName = string.Format("工时计算");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkCalculate.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkMachine.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnMachineTime");
                subInfo.FunName = string.Format("机器时间");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkMachine.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkSJ.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnSJ");
                subInfo.FunName = string.Format("BOM_SJ");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkSJ.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkBOM_STD.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnSTD");
                subInfo.FunName = string.Format("BOM_STD");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkBOM_STD.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkUnCheck.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnUnCheck");
                subInfo.FunName = string.Format("未审核");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkUnCheck.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkCheck.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnUnCheck");
                subInfo.FunName = string.Format("已审核");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkCheck.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkIECalculate.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnIECalcuate");
                subInfo.FunName = string.Format("IE+计算");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkReturn.Tag;

                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkCalUpLoad.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnIEExportUpLoad");
                subInfo.FunName = string.Format("IE+上载");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkReturn.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkReturn.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnReturn");
                subInfo.FunName = string.Format("返回");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkReturn.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkUpLoadSap.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnUpLoadSAP");
                subInfo.FunName = string.Format("上载SAP");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkUpLoadSap.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkImport_Feature.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnFeatureImport");
                subInfo.FunName = string.Format("特征导入");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkImport_Feature.Tag;            
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }

            if (chkApproval.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnApproval");
                subInfo.FunName = string.Format("审批");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkApproval.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkDeclare.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnDeclare");
                subInfo.FunName = string.Format("申报");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkDeclare.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            if (chkBack.Checked)
            {
                subInfo = CreateSubFunction(mainInfo);
                subInfo.SortCode = (sortCodeIndex++).ToString("D2");
                subInfo.FunControlID = string.Format("btnBack");
                subInfo.FunName = string.Format("退回");
                subInfo.CreateDate = DateTime.Now;
                subInfo.EngName = (string)chkBack.Tag;
                BaseFactory<Sys_Function>.Instance.Insert(subInfo);
            }
            #endregion

            //trans.Commit();
            //ProcessDataSaved(this.btnSave, new EventArgs());

            //this.DialogResult = System.Windows.Forms.DialogResult.OK;
            MessageBox.Show ("保存成功");
            this.Close();
                        //}
                        //else
                        //{
                        //    MessageBox.Show("保存失败");
                        //}
                    //}
            //    }
            //    catch (Exception ex)
            //    {
            //        if (trans != null)
            //        {
            //            trans.Rollback();
            //        }

                    
            //        MessageDxUtil.ShowError(ex.Message);
            //    }
            //}
        }
        private Sys_FunctionInfo CreateSubFunction(Sys_FunctionInfo mainInfo)
        {
            Sys_FunctionInfo subInfo = new Sys_FunctionInfo();
            subInfo.PID = mainInfo.ID;
            subInfo.SystemType = mainInfo.SystemType;
            return subInfo;
        }

        private Sys_FunctionInfo SetFunction(Sys_FunctionInfo info)
        {
            //info.FunName = this.txtName.Text;
            //info.PID = this.ucFunction.Value;
            //info.FunControlID = this.txtFunctionID.Text;
            return info;
        }

        private void chkNormal_CheckedChanged(object sender, EventArgs e)
        {
            
            //if (chkNormal.Checked) chkAll.Checked = false;
            //else chkAll.Checked = true;
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach(Control c in groupBox2.Controls)
            {
                if(c is CheckEdit)
                {
                    CheckEdit uc = c as CheckEdit;
                    uc.Checked = false ;
                }
            }
        }

        private void chkNotALL_CheckedChanged(object sender, EventArgs e)
        {
            //foreach (Control c in groupBox2.Controls)
            //{
            //    if (c is CheckEdit)
            //    {
            //        CheckEdit uc = c as CheckEdit;
            //        uc.Checked = false;
            //    }
            //}
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            chkAdd.Checked = true;
            chkDelete.Checked = true;
            chkModify.Checked = true;
            chkExport.Checked = true;
            chkImport.Checked = true;
            chkPrint.Checked = true;
            chkView.Checked = true;
            chkReturn.Checked = true;
            chkSave.Checked = true;
            chkCalUpLoad.Checked = true;
            chkIECalculate.Checked = true;
            chkUpLoadSap.Checked = true;
        }

        private void radALL_CheckedChanged(object sender, EventArgs e)
        {
            foreach (Control c in groupBox2.Controls)
            {
                if (c is CheckEdit)
                {
                    CheckEdit uc = c as CheckEdit;
                    uc.Checked = true;
                }
            }
        }
    }
}
